//
//  DSXMasterViewController.m
//  iDerbyOSSampler
//
//  Created by glenn andreas on 1/7/14.
//  Copyright (c) 2014 glenn andreas. All rights reserved.
//

#import "DSXMasterViewController.h"

#import "DSXDetailViewController.h"
#import "DSXRosterViewController.h"

#import "DerbyNSOde.h"
#import "DXMLBoutObject.h"
#import "DXMLTeamObject.h"

@interface DSXMasterViewController () {
    NSMutableArray *_objects;
}
@end

@implementation DSXMasterViewController

- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table View
enum {
    SectionMain = 0,
    SectionTeams,
    SectionCount,
    
    RowMainScoreboard = 0,
    RowMainCount,
    
    RowTeamHome = 0,
    RowTeamAway,
    RowTeamCount,
};

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return SectionCount;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case SectionMain:
            return RowMainCount;
        case SectionTeams:
            return RowTeamCount;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    switch (indexPath.section) {
        case SectionMain:
            switch (indexPath.row) {
                case RowMainScoreboard:
                    cell.textLabel.text = @"Scoreboard";
                    break;
            }
            break;
        case SectionTeams:
            switch (indexPath.row) {
                case RowTeamHome:
                    cell.textLabel.text = @"Home Roster";
                    break;
                case RowTeamAway:
                    cell.textLabel.text = @"Away Roster";
                    break;
                    
                default:
                    break;
            }
            break;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case SectionMain:
            switch (indexPath.row) {
                case RowMainScoreboard:
                    [self performSegueWithIdentifier:@"Scoreboard" sender:self];
                    break;
            }
            break;
        case SectionTeams:
            [self performSegueWithIdentifier:@"Roster" sender:self];
            break;
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [[segue destinationViewController] setNsode:self.nsode];
    
    if ([[segue identifier] isEqualToString:@"Roster"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        if (indexPath.row == RowTeamHome) {
            [[segue destinationViewController] setTeam:self.nsode.xml.homeTeam];
        } else if (indexPath.row == RowTeamAway) {
            [[segue destinationViewController] setTeam:self.nsode.xml.awayTeam];
        }
    }
}

@end
